<template>
  <el-card class="box-card">
    <el-form :model="editForm" ref="editForm">
      <el-form-item label="版本" prop="version"
                    :rules="{required: true, message: '请输入版本', trigger: 'blur'}">
        <el-input v-model="editForm.version" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content" :rules="[{required: true, message: '请输入内容', trigger: 'blur'}]">
        <el-input type="textarea" v-model="editForm.content"></el-input>
      </el-form-item>
      <el-form-item label="强制更新">
        <el-switch v-model="forceValue" on-color="#13ce66" off-color="#ff4949" on-text="是" off-text="否">
        </el-switch>
      </el-form-item>
      <el-form-item label="android链接" prop="androidUrl"
                    :rules="[{required: true, message: '请输入android链接', trigger: 'blur'}]">
        <el-input v-model="editForm.androidUrl"></el-input>
      </el-form-item>
      <el-form-item label="ios链接" prop="iosUrl" :rules="[{required: true, message: '请输入ios链接', trigger: 'blur'}]">
        <el-input v-model="editForm.iosUrl"></el-input>
      </el-form-item>
    </el-form>
    <el-button @click="submit">确定</el-button>
    <el-button @click="back">返回</el-button>
  </el-card>
</template>
<style>

</style>
<script>
export default{
  created() {
    this.loadGridData();
  },
  data(){
    return{
       editForm:{
          version:'',
          content:'',
          forceUpdate:'',
          androidUrl:'',
          iosUrl:''
       },
    }
  },
        computed:{
          forceValue:{
             get:function(){
               return this.editForm.forceUpdate === 1
             },
             set:function(newValue){
                if(newValue)
                this.editForm.forceUpdate=1
                else
                this.editForm.forceUpdate=0
             }
          }
        },
  methods:{
    loadGridData() {
      let id=this.$route.params.id
      this.isNewPage = id === undefined
      if (this.isNewPage) {
         return
      }
      else{
         this.axios.get(`version_infos/${id}`).then(response=>{
           this.editForm=response.data
         })
      }
    },
    back(){
      this.$router.push(`/version_infos`)
      },
    submit(){
		  this.$refs.editForm.validate((valid) => {
				if (!valid) {
				  return
				}
			  this.$confirm('确认提交吗？','提示',{
			      cancelButtonText: '取消',
            confirmButtonText: '确定',
			  }).then(() => {
				  this.editLoading = true
          if (this.isNewPage) {
            this.axios.post("/version_infos", this.editForm).then(response => {
              this.$notify({
                title: '成功',
                message: '提交成功',
                type: 'success'
              })
            }).then(()=>{this.back()})
          } else{
						    let id=this.$route.params.id
							  this.axios.patch(`version_infos/${id}`,this.editForm).then(response=>{
							    this.$notify({
                    title: '成功',
                    message: '提交成功',
                    type: 'success'
								  });
							  }).then(()=>{this.back()})
								this.editLoading = false
				    }
				})
      })
    }
  },
}

</script>
